<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="130px" :rules="dynamicFormRules">
                <el-form-item label="模板编号" v-show="isEditShow" prop="t_id">
                    <el-input v-model="formData.t_id" disabled placeholder="请输入" />
                </el-form-item>
                <el-form-item label="模板名称" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="是否默认" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择是否默认">
                        <el-radio :label="1">默认</el-radio>
                        <el-radio :label="0">选择</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="运营能否选择" prop="is_show">
                    <el-radio-group v-model="formData.is_show" placeholder="运营能否选择">
                        <el-radio :label="1">可以选择</el-radio>
                        <el-radio :label="0">不能选择</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  rechargeGearTypeEdit, rechargeGearTypeAdd, rechargeGearTypeDetail } from '@/api/client/recharge_gear_type'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑充值档位' : '新增充值档位'
})
const isEditShow = computed(() => mode.value === 'edit')
const formData = reactive({
    t_id: 0,
    title: '',
    status: 0,
    is_show: 1,
})

const formRules = {
    t_id: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    title: [
        {
            required: true,
            message: '请输入标题',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择是否默认 1 默认 0 不默认',
            trigger: ['blur']
        }
    ],
    is_show: [
        {
            required: true,
            message: '请选择是否可以选择 1 是 0 不能选择',
            trigger: ['blur']
        }
    ],
}

// 修改为动态规则
const dynamicFormRules = computed(() => {
    const rules = { ...formRules }

    // 在添加模式下，series_id 不需要必填
    if (mode.value === 'add') {
        rules.t_id = [
            {
                required: false,
                message: '请输入模板编号',
                trigger: ['blur']
            }
        ]
    }

    return rules
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await rechargeGearTypeEdit(data) : await rechargeGearTypeAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await rechargeGearTypeDetail({
        t_id: row.t_id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
